﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;


namespace Magenic.CodeMastery.SharePoint.PMO
{
    public class GraphComposer
    {
        //        private DateTime testDate;

        private int columnsPerMonth = 4;

        private DisplayPeriod display;

        public GraphComposer(DateTime dayInFirstMonth, int monthsToDisplay)
        {
            display = new DisplayPeriod(dayInFirstMonth, monthsToDisplay);

        }

        public String Render()
        {
            StringBuilder markup = new StringBuilder();
            //See http://www.tek-tips.com/faqs.cfm?fid=4499
            //for fixed-width tables
            markup.Append("<table style=\"{table-layout:fixed}\">");
            for (int i = 0; i < this.display.Months * columnsPerMonth; ++i)
                markup.Append("<col width=\"16px\"/>");
            markup.Append(composeBody(columnsPerMonth));
            markup.Append("</table>");
            return markup.ToString();
        }

        private String composeBody(int columnsPerMonth)
        {
            List<Project> projects = new List<Project>();
            ProjectRepository repos = new ProjectRepository();
            Project[] items = repos.GetProjectsOrderedForDisplay();
            projects.AddRange(items);

            int index = 0;
            StringBuilder markup = new StringBuilder();
            markup.Append("<table>");
            markup.Append(composeOuterHeaderRow(columnsPerMonth));
            while (index < items.Length)
            {
                String row = composeProjectsForManager(items, ref index);
                markup.Append(row);
            }
            markup.Append("</table>");
            return markup.ToString();
        }

        public String composeOuterHeaderRow(int columnsPerMonth)
        {
            HeaderComposer hdr = new HeaderComposer(display.FirstDay,
                display.Months);
            StringBuilder html = new StringBuilder("<tr><td></td><td>");
            html.Append(openRhsTable());
            html.Append(hdr.composeYearHeaderRow(columnsPerMonth));
            html.Append(hdr.composeMonthHeaderRow(columnsPerMonth));
            html.Append("</table></td></tr>");
            return html.ToString();
        }

        private String openRhsTable()
        {
            StringBuilder html = new StringBuilder();
            //See http://www.tek-tips.com/faqs.cfm?fid=4499
            //for fixed-width tables
            html.Append("<table style=\"{table-layout:fixed}\">");
            for (int i = 0; i < display.Columns; ++i)
                html.Append("<col width=\"16px\"/>");
            return html.ToString();
        }

        /// <summary>
        /// NOTE:  This assumes an array sorted by PM first!
        /// </summary>
        /// <param name="projects"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public String composeProjectsForManager(Project[] projects, ref int index)
        {
            String pmName = projects[index].Manager;

            StringBuilder html = new StringBuilder();
            html.Append("<tr><td style=\"{border:1px solid red;}\">");
            html.Append(HttpUtility.HtmlEncode(pmName));
            html.Append("</td><td style=\"{border:1px solid yellow;}\">");
            ////See http://www.tek-tips.com/faqs.cfm?fid=4499
            ////for fixed-width tables
            //html.Append("<table style=\"{table-layout:fixed}\">");
            //for (int i = 0; i < display.Columns; ++i)
            //    html.Append("<col width=\"16px\"/>");
            html.Append(openRhsTable());

            int count = 0;
            for (; index < projects.Length; ++index)
            {
                Project prj = projects[index];

                //render projects for current PM only
                String testName = prj.Manager;
                if (!pmName.Equals(testName))
                    break;

                //skip projects outside display window
                if (!display.OverlapsWithProject(prj))
                    continue;

                String row = composeProjectBar(prj);
                html.Append(row);
                ++count;
            }

            if (count > 0)
                html.Append("</table></td></tr>");
            else
                html.Length = 0;
            return html.ToString();
        }


        /// <summary>
        /// This composes the leading space, the project and the trailing
        /// space.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public String composeProjectBar(Project project)
        {
            //int startColumn = display.StartColumn(project) - 1;
            //int finishColumn = display.FinishColumn(project) - 1;

            //StringBuilder markup = new StringBuilder();
            //markup.Append("<tr>");
            //if (startColumn > 0)
            //    markup = renderEmpty(markup, startColumn);
            //markup = renderProject(markup, finishColumn -
            //    startColumn + 1, project);
            //int remainder = display.Columns - finishColumn - 1;
            //if (remainder > 0)
            //    markup = renderEmpty(markup, remainder);
            //markup.Append("</tr>");

            TableBasedRow row = new TableBasedRow(display);
            try
            {

                row.Add(project);
            }
            catch (Exception /*ex*/)
            {
            }
            row.AddTrailingColumns();
            return row.ToString();
        }
    }
}